import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/openeye/config/color.dart';
import 'package:flutter_app/openeye/config/string.dart';
import 'package:flutter_app/openeye/http/http_manager.dart';
import 'package:flutter_app/openeye/page/home/home_page.dart';
import 'package:flutter_app/openeye/utils/toast_utils.dart';
import 'package:flutter_app/openeye/viewmodel/tab_navigation_viewmodel.dart';
import 'package:flutter_app/openeye/widget/provider_widget.dart';
import 'package:provider/provider.dart';

class TabNavigation extends StatefulWidget {
  const TabNavigation({Key? key}) : super(key: key);

  @override
  _TabNavigationState createState() => _TabNavigationState();
}

class _TabNavigationState extends State<TabNavigation> {
  final PageController _pageController = PageController();
 late DateTime lastTime;

  @override
  Widget build(BuildContext context) {
    var tabNavigationViewModel = TabNavigationViewModel();

    return WillPopScope(
        child: Scaffold(
          body: PageView(
            controller: _pageController,
            onPageChanged: (index){tabNavigationViewModel.changeBottomTabIndex(index);},
            children: [
               const HomePage(),
              Container(color: LeoColor.find,),
              Container(color: LeoColor.hot,),
              Container(color: LeoColor.me,),
            ],
            // onPageChanged: model.,
          ),
          bottomNavigationBar:
            ProviderWidget<TabNavigationViewModel>(
              model: tabNavigationViewModel,
              builder: (context,model,child){
                return BottomNavigationBar(
                  currentIndex: model.currentIndex,
                  type: BottomNavigationBarType.fixed,
                  selectedItemColor: const Color(0xff000000),
                  unselectedItemColor: const Color(0xff9a9a9a),
                  items: _item(),
                  onTap: (index){
                    debugPrint("点击了底部导航栏");
                    if(model.currentIndex != index){
                      _pageController.jumpToPage(index);
                      model.changeBottomTabIndex(index);
                    }
                  },
                );
              },
            )

        ),
        onWillPop: _onWillPop);
  }


  Future<bool> _onWillPop() async {
    if (lastTime == null ||
        DateTime.now().difference(lastTime) > const Duration(seconds: 2)) {
      lastTime = DateTime.now();
      ToastUtils.showTip(LeoString.back_app);
      return false;
    } else {
      return true;
    }
  }

  List<BottomNavigationBarItem> _item() {
    return [
      _bottomIte(LeoString.home, 'assets/images/ic_home_normal.png',
          'assets/images/ic_home_selected.png'),
      _bottomIte(LeoString.discovery, 'assets/images/ic_discovery_normal.png',
          'assets/images/ic_home_selected.png'),
      _bottomIte(LeoString.hot, 'assets/images/ic_hot_normal.png',
          'assets/images/ic_home_selected.png'),
      _bottomIte(LeoString.mine, 'assets/images/ic_mine_normal.png',
          'assets/images/ic_home_selected.png'),
    ];
  }

  _bottomIte(String title, String normalIcon, String selectorIcon) {
    return BottomNavigationBarItem(
        icon: Image.asset(
          normalIcon,
          width: 24,
          height: 24,
        ),
        activeIcon: Image.asset(
          selectorIcon,
          width: 24,
          height: 24,
        ),
        label: title);
  }
}
